<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      label-width="68px"
      size="small"
    >
      <el-form-item
        label-width="auto"
        :label="$t('productName')"
        prop="productName"
      >
        <el-input
          v-model="queryParams.productName"
          clearable
          :placeholder="$t('pleaseEnterTheProductName')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:protype:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
          >{{ $t("add") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-sort"
          plain
          size="mini"
          type="info"
          @click="toggleExpandAll"
          >{{ $t("unfold") }}/{{ $t("packUp") }}
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="protypeList"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      row-key="productId"
    >
      <!--   -->
      <el-table-column
        align="center"
        :label="$t('productName')"
        prop="productName"
      />

      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        :label="$t('controls')"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:protype:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >{{ $t("edit") }}
          </el-button>
          <el-button
            v-hasPermi="['system:protype:add']"
            icon="el-icon-plus"
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
            >{{ $t("add") }}
          </el-button>
          <el-button
            v-hasPermi="['system:protype:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            >{{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改产品类别对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      append-to-body
      center
      width="500px"

    >
      <el-form
        ref="form"
        :model="form"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item :label="$t('productType')" prop="productName">
          <el-input
            v-model="form.productName"
            :placeholder="$t('pleaseEnterTheProductCategory')"
          />
        </el-form-item>
        <el-form-item :label="$t('remark')" prop="remark">
          <el-input
              v-model="form.remark"
              :placeholder="$t('input')"
          />
        </el-form-item>
        <el-form-item :label="$t('parentClass')" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :normalizer="normalizer"
            :options="protypeOptions"
            placeholder="$t('pleaseElectAPublicProperty')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t("ok") }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addProtype,
  delProtype,
  getProtype,
  listAttribute,
  listProtype,
  updateProtype,
} from "@/api/system/protype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Protype",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 产品类别表格数据
      protypeList: [],
      // 产品类别树选项
      protypeOptions: [],
      attributeOptionsAttribute: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
    rules: {
      productName: [
        {required: true, message: this.$t('menu188'), trigger: "blur"}
      ],
      remark: [
        {required: true, message: this.$t('menu189'), trigger: "blur"}
      ],
    },
      // 查询参数
      queryParams: {
        parentId: null,
        ancestors: null,
        productName: null,
        orderNum: null,
        phone: null,
        email: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品类别列表 */
    getList() {
      this.loading = true;
      listProtype(this.queryParams).then((response) => {
        this.protypeList = this.handleTree(
          response.data,
          "productId",
          "parentId"
        );
        this.loading = false;
      });
    },
    /** 转换产品类别数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.productId,
        label: node.productName,
        children: node.children,
      };
    },
    /** 查询产品类别下拉树结构 */
    getTreeselect() {
      listProtype().then((response) => {
        this.protypeOptions = [];
        const data = {
          productId: 0,
          productName: this.$t("top-node"),
          children: [],
        };
        data.children = this.handleTree(response.data, "productId", "parentId");
        this.protypeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        parentId: null,
        ancestors: null,
        productName: null,
        orderNum: null,

        phone: null,
        email: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attributeIds: [],
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();

      if (row != null && row.productId) {
        this.form.parentId = row.productId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = this.$t("addTheProductCategory");
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();

      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getProtype(row.productId).then((response) => {
        this.form = response.data;
        // this.form.remark = this.form.remark.split(",");
        this.open = true;
        this.title = this.$t("alterTheProductCategory");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.remark = this.form.remark.join(",");
          console.log(this.form);
          if (this.form.productId != null) {
            updateProtype(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("modifiedSuccessfully"));
              this.open = false;
              this.getList();
            });
          } else {
            addProtype(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("newSuccessfullyAdded"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm(
          this.$t("areYouSureYouWantToDeleteTheDataItemWithTypeNumber") +
            '"' +
            row.productName +
            '"'
        )
        .then(function () {
          return delProtype(row.productId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(thsi.$t("deleteSuccessfully"));
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
>>> .el-table__indent {
  margin-left: 70px !important;
}
</style>
